/**
 * PsInstallFilter.java

 * @author 
 * @since 2017年6月9日
 */
package com.mall.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.mall.client.user.UserService;
import com.mall.pojo.User;
import com.mall.web.util.HttpUrlTypeUtil;
import com.mall.web.util.HttpUtil;

/**
 * @author 
 *
 */
public class PsInstallFilter implements Filter {

	@Autowired
	private UserService userService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String uri = request.getRequestURI();
		if (StringUtils.isNotBlank(uri)) {
			if(uri.contains("addPicjson") || uri.contains("error") ||uri.contains("psLogin")||uri.contains("psIndex")||uri.contains("userChoiceAction") ){
				arg2.doFilter(request, response);
				return;
			}
		}
		
		int psInstall = HttpUrlTypeUtil.isPsIstallUrl(request);
		if (psInstall == 0) {
			arg2.doFilter(request, response);
			return;
		}

		Long userId = HttpUtil.getLongValueFromRequest(request, "userId");
		Long customerId = HttpUtil.getLongValueFromRequest(request, "customerId");
		Long installerId = HttpUtil.getLongValueFromRequest(request, "installerId");
		Long loginUserId = 0l;
		if (userId != null && userId > 0) {
			loginUserId = userId;
		}
		if (customerId != null && customerId > 0) {
			loginUserId = customerId;
		}
		if (installerId != null && installerId > 0) {
			loginUserId = installerId;
		}
		// 如果没有用户id，需要跳转到错误页面
		if (loginUserId == 0l) {
			request.getRequestDispatcher("/ps/task/error.htm").forward(request, response);
			return;
		}

		User user = this.userService.getUserByUserId(loginUserId);
		// 如果没有用户id，需要跳转到错误页面
		if (loginUserId == 0l || user == null || user.getStatus() != 2) {
			request.getRequestDispatcher("/ps/task/nouseriderror.htm").forward(request, response);
			return;
		}
//		if (loginUserId == 0l || user == null) {
//			request.getRequestDispatcher("/ps/task/nouseriderror.htm").forward(request, response);
//			return;
//		}

		arg2.doFilter(request, response);
		return;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
